<?php

namespace app\helps;
use think\Db;

class CreateSell{


    /**
     * 系统放单
     * @param $good 商品对象
     * @param $sysMobille
     * @param $money
     */
    public function createSysSell($good,$money) {
        $sysBank =$this->getRandSysBank();

        if (!$sysBank){
            return "没有可用的系统收款";
        }
        $data=[];

        $now = time();
        $outTime =$now+24*3600*$good["days"];
        $outDate =date('Y-m-d',$now)." ".$good["lottery_time"];
        $data["create_time"]=date('Y-m-d H:i:s');
        $data["end_time"]= $data["create_time"];
        $data["estimate"]= $data["create_time"];
        $data["type"]=1;//1 平台放单 2 换购订单 3 用户订单
        $data["uid"]=0;
        $data["user_name"]=$sysBank["user_name"];
        $data["mobile"]=$sysBank["mobile"];
        $data["nick_name"]=$sysBank["user_name"];
        $data["gid"]=$good["id"];
        $data["gname"]=$good["name"];
        $data["mt"]=$good["mt"];
        $data["day_profit"]=$good["day_profit"];
        $data["days"]=$good["days"];
        $data["price"]=$money;
        $data["sys_bank"]=$sysBank["id"];
        $data["status"]=1;
        $data["sn"]=$this->getSn();
//        return json_encode($data);
        $id = Db::table("tb_mt_sell")->insertGetId($data);
        if ($id<=0){
            return "入库失败";
        }
        //订单修改记录
        $record= "出售订单：创建成功";
        $mark = "系统订单";
        $resRecord = $this->create_order_record($id,$data["sn"],$record,$mark);
        if ($resRecord){
            return 1;
        }
        return "记录订单出错";
    }

    /**
     * 系统放单
     * @param $good 商品对象
     * @param $sysMobille
     * @param $money
     */
    public function createSysSellForUser($good,$money,$user) {
        $data=[];
        $now = time();
        $outTime =$now+24*3600*$good["days"];
        $outDate =date('Y-m-d',$now)." ".$good["lottery_time"];
        $data["create_time"]=date('Y-m-d H:i:s');
        $data["end_time"]= $data["create_time"];
        $data["estimate"]= $data["create_time"];
        $data["type"]=1;//1 平台放单 2 换购订单 3 用户订单
        $data["uid"]=$user["id"];
        $data["user_name"]=$user["username"];
        $data["mobile"]=$user["mobile"];
        $data["nick_name"]=$user["nickname"];
        $data["gid"]=$good["id"];
        $data["gname"]=$good["name"];
        $data["mt"]=$good["mt"];
        $data["day_profit"]=$good["day_profit"];
        $data["days"]=$good["days"];
        $data["price"]=$money;
        $data["sys_bank"]=0;
        $data["status"]=1;
        $data["sn"]=$this->getSn();
//        return json_encode($data);
        $id = Db::table("tb_mt_sell")->insertGetId($data);
        if ($id<=0){
            return "入库失败";
        }
        //订单修改记录
        $record= "出售订单：创建成功";
        $mark = "系统订单";
        $resRecord = $this->create_order_record($id,$data["sn"],$record,$mark);
        if ($resRecord){
            return 1;
        }
        return "记录订单出错";
    }

    /**
     * 订单确认 给卖家生成新订单
     * @param $view_order 订单对象
     */
    public function createUserSell($view_order) {
        $good = Db::table("tb_mt_good")->where("id",$view_order["sell_gid"])->find();
        $tb_user = Db::table("tb_user")->where("id",$view_order["buy_id"])->find();
        $data=[];
        $now = time();
        $outTime =$now+24*3600*$view_order["sell_days"];
        $outDate =date('Y-m-d',$outTime)." ".$good["lottery_time"];
        $data["create_time"]=date('Y-m-d H:i:s');
        $data["end_time"]=$outDate;
        $data["estimate"]=$outDate;
        $data["type"]=3;//1 平台放单 2 换购订单 3 用户订单
        $data["uid"]=$tb_user["id"];
        $data["user_name"]=$tb_user["username"];
        //修改成紧急联系人
        $data["mobile"]=$tb_user["mobile"];
        $data["nick_name"]=$tb_user["contact"];
        $data["gid"]=$good["id"];
        $data["gname"]=$good["name"];
        $data["mt"]=$good["mt"];
        $data["day_profit"]=$good["day_profit"];
        $data["days"]=$good["days"];
        $data["price"]=$view_order["sell_price"];
        $data["sys_bank"]=0;
        $data["status"]=1;
        $new_sn = $this->getSn();
        $data["sn"]=$new_sn;
        $id = Db::table("tb_mt_sell")->insertGetId($data);
        if ($id<=0){
            return "入库失败";
        }
        //订单修改记录
        $record= "出售订单：创建成功";
        $mark = "用户订单：lastSn：".$view_order["sell_sn"];
        $resRecord = $this->create_order_record($id,$data["sn"],$record,$mark);
        if (!$resRecord){
            return "订单修改记录失败";
        }
        //改变 出售表状态 以及 更新接盘人
        //1 收益中 2 匹配中 3 用户取消再次待卖 4 申述成功再次待卖 5销毁 6判定代卖 7 完成
        $updateSell = [];
        $updateSell["status"]=7;
        $updateSell["next_id"]=$id;
        $updateSell["next_sn"]=$new_sn;
        $updateSell["next_uid"]=$tb_user["id"];
        $updateSell["next_user_name"]=$tb_user["username"];
        $resSell =  Db::table("tb_mt_sell")->where("id",$view_order["sell_id"])->update($updateSell);
        if (!$resSell){
            return "出售表更新失败";
        }
        return 1;
    }

    /**
     * 换购订单 给卖家生成新订单
     * @param $good 商品对象
     * @param $user 换购用户
     * @param $price 换购金额
     */
    public function createUserExChangeSell($user,$good,$price) {
      ;
        $data=[];
        $now = time();
//        $outTime =$now+0*3600*$good["days"];
//        $outDate =date('Y-m-d',$outTime)." ".$good["lottery_time"];
        $data["create_time"]=date('Y-m-d H:i:s');
        $data["end_time"]= $data["create_time"];
        $data["estimate"]= $data["create_time"];
        $data["type"]=2;//1 平台放单 2 换购订单 3 用户订单
        $data["uid"]=$user["id"];
        $data["user_name"]=$user["username"];
        $data["mobile"]=$user["mobile"];
        $data["nick_name"]=$user["contact"];
        $data["gid"]=$good["id"];
        $data["gname"]=$good["name"];
        $data["mt"]=$good["mt"];
        $data["day_profit"]=$good["day_profit"];
        $data["days"]=$good["days"];
        $data["price"]=$price;
        $data["sys_bank"]=0;
        $data["status"]=1;
        $data["sn"]=$this->getSn();
        $id = Db::table("tb_mt_sell")->insertGetId($data);
        if ($id<=0){
            return "入库失败";
        }
        //订单修改记录
        $record= "出售订单：创建成功";
        $mark = "换购订单：price：".$price;
        $resRecord = $this->create_order_record($id,$data["sn"],$record,$mark);
        if (!$resRecord){
            return "记录订单出错";
        }
        //换购记录表
        $exRrecordData =[];
        $exRrecordData["time"]=date('Y-m-d H:i:s');
        $exRrecordData["gid"]=$good["id"];
        $exRrecordData["gname"]=$good["name"];
        $exRrecordData["gmark"]=$good["min"]."-".$good["max"];
        $exRrecordData["mt"]=$good["exchange_mt"];
        $exRrecordData["type"]=$good["exchange"];
        $exRrecordData["num"]=$price;
        $exRrecordData["uid"]=$user["id"];
        $exRrecordData["user_name"]=$user["username"];
        $exRrecordData["sell_sn"]=$data["sn"];
        $exRrecordData["sell_id"]=$id;
        $exRrecordRes =Db::table("tb_mt_ex_goods")->insert($exRrecordData);
        if (!$exRrecordRes){
            return "换购记录出错";
        }
        return 1;
    }

    /**
     * 换购订单 给卖家生成新订单
     * @param $good 商品对象
     * @param $user 换购用户
     * @param $price 换购金额
     */
    public function createUserSellByAward($user,$good,$price) {
        ;
        $data=[];
        $now = time();
//        $outTime =$now+0*3600*$good["days"];
//        $outDate =date('Y-m-d',$outTime)." ".$good["lottery_time"];
        $data["create_time"]=date('Y-m-d H:i:s');
        $data["end_time"]=date('Y-m-d')." 23:59:59";
        $data["estimate"]= date('Y-m-d')." 23:59:59";
        $data["type"]=4;//1 平台放单 2 换购订单 3 用户订单 4 九宫格抽奖
        $data["uid"]=$user["id"];
        $data["user_name"]=$user["username"];
        $data["mobile"]=$user["mobile"];
        $data["nick_name"]=$user["contact"];
        $data["gid"]=$good["id"];
        $data["gname"]=$good["name"];
        $data["mt"]=$good["mt"];
        $data["day_profit"]=$good["day_profit"];
        $data["days"]=$good["days"];
        $data["price"]=$price;
        $data["sys_bank"]=0;
        $data["status"]=1;
        $data["sn"]=$this->getSn();
        $id = Db::table("tb_mt_sell")->insertGetId($data);
        if ($id<=0){
            return "入库失败";
        }
        //订单修改记录
        $record= "出售订单：创建成功";
        $mark = "换购订单：price：".$price;
        $resRecord = $this->create_order_record($id,$data["sn"],$record,$mark);
        if (!$resRecord){
            return "记录订单出错";
        }
        //换购记录表
        $exRrecordData =[];
        $exRrecordData["time"]=date('Y-m-d H:i:s');
        $exRrecordData["gid"]=$good["id"];
        $exRrecordData["gname"]=$good["name"];
        $exRrecordData["gmark"]=$good["min"]."-".$good["max"];
        $exRrecordData["mt"]=$good["exchange_mt"];
        $exRrecordData["type"]=$good["exchange"];
        $exRrecordData["num"]=$price;
        $exRrecordData["uid"]=$user["id"];
        $exRrecordData["user_name"]=$user["username"];
        $exRrecordData["sell_sn"]=$data["sn"];
        $exRrecordData["sell_id"]=$id;
        $exRrecordRes =Db::table("tb_mt_ex_goods")->insert($exRrecordData);
        if (!$exRrecordRes){
            return "换购记录出错";
        }
        return 1;
    }

    public function order_destroy($sell){
        $id = $sell["id"];
        $updateData=[];
        $updateData["status"] =5;
        $res = Db::table("tb_mt_sell")->where('id', $id)->update($updateData);
        if (!$res){
            return "订单销毁出错";
        }
        $record= "出售表订单销毁成功";
        $mark = "系统人员销毁";
        $resRecord = $this->create_order_record($id,$sell["sn"],$record,$mark);
        if ($resRecord){
            return 1;
        }
        return "记录订单出错";
    }

    /**
     * 随机获取系统银行卡
     * @return |null
     */
    private function getRandSysBank(){
        $banks =Db::table("tb_mt_bank")->where("uid",0)->whereIn("type","1,2,3")->where("status",1)->select();
        if (!$banks){
            return null;
        }
        $size = count($banks);
        if ($size){
            $rand=rand(0,$size-1);
            return $banks[$rand];
        }
        return null;
    }

    /**
     * 订单号
     * @return string
     */
    private function getSn(){
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        return $orderSn;
    }


    /**
     * 创建订单修改记录
     * @param $order
     */
    public function create_order_record($oid,$sn,$record,$mark="") {
        $data =[];
        $data["oid"]=$oid;
        $data["sn"]=$sn;
        $data["record"]=$record;
        $data["mark"]=$mark;
        $data["time"]=date('Y-m-d H:i:s');
        return  Db::table("tb_mt_order_record")->insert($data);

    }



}